<script setup lang="ts">
import { ref } from 'vue'
import { showToast, showLoadingToast } from 'vant'
import { payConsultOrder } from '@/services/consult'
const props = withDefaults(
  defineProps<{
    show: boolean
    actualPayment: number
    orderId: string
    callbackUrl?: string
  }>(),
  {
    callbackUrl: '/room'
  }
)

const paymentMethod = ref<0 | 1>()

const emits = defineEmits<{
  (e: 'update:show', isShow: boolean): void
}>()
const handleClose = () => {
  // 面板关闭的时候，需要通知父组件，设置父组件变量isShow为false
  emits('update:show', false)
}

// 支付
const pay = async () => {
  if (!paymentMethod.value) return showToast('请选择支付方式')
  // 跳转支付loading加载
  // duration: 0 表示一直存在
  // forbidClick: true // 禁止点击
  showLoadingToast({
    message: '支付中...',
    forbidClick: true,
    duration: 0
  })
  // 调用支付接口
  const { data } = await payConsultOrder({
    paymentMethod: paymentMethod.value,
    orderId: props.orderId,
    // payCallback表示支付成功后的跳转地址 'http://localhost:5173/room'
    payCallback: 'http://localhost:5173' + props.callbackUrl
  })
  window.location.href = data.payUrl
}
</script>
<template>
  <van-action-sheet
    :closeable="false"
    :show="show"
    title="选择支付方式"
    :close-on-popstate="false"
    @close="handleClose"
  >
    <div class="pay-type">
      <p class="amount">￥{{ actualPayment.toFixed(2) }}</p>
      <van-cell-group>
        <van-cell title="微信支付" @click="paymentMethod = 0">
          <template #icon><cp-icon name="consult-wechat" /></template>
          <template #extra
            ><van-checkbox :checked="paymentMethod === 0"
          /></template>
        </van-cell>
        <van-cell title="支付宝支付" @click="paymentMethod = 1">
          <template #icon><cp-icon name="consult-alipay" /></template>
          <template #extra
            ><van-checkbox :checked="paymentMethod === 1"
          /></template>
        </van-cell>
      </van-cell-group>
      <div class="btn">
        <van-button type="primary" round block @click="pay"
          >立即支付</van-button
        >
      </div>
    </div>
  </van-action-sheet>
</template>

<style scoped>
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
